/*
 * UIEligeDireccion.java
 *
 * Created on 8 de febrero de 2008, 17:34
 */

package vista;

import Datos.Baja;
import Datos.Consulta;
import Datos.Modificacion;
import controlador.CAlta;
import controlador.CListar;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import negocios.Departamento;
import negocios.Localidad;
import negocios.Provincia;
import reportes.cExport_thread;

/**
 *
 * @author  Administrador
 */
public class UILocalidades2 extends javax.swing.JFrame {
    private Collection provincias = new ArrayList();
    private Collection departamentos = new ArrayList();
    private Collection localidades = new ArrayList();
    
    private boolean guardar = false;
    
    
    
    /** Creates new form UIEligeDireccion */
    public UILocalidades2() {
        initComponents();
        Dimension pantalla, cuadro;
	pantalla = Toolkit.getDefaultToolkit().getScreenSize();
	cuadro = this.getSize();
	this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
        this.llenarProvincias();
    }
    
    
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jPanel1 = new javax.swing.JPanel();
        jCProvincia = new javax.swing.JComboBox();
        jCDepartamento = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jBAbreProvincias = new javax.swing.JButton();
        jBAbreDepartamentos = new javax.swing.JButton();
        jTNombreLocalidad1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jBGuardar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jBEliminar = new javax.swing.JButton();
        jBImprimir = new javax.swing.JButton();
        jBModificar = new javax.swing.JButton();
        jBNuevo = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Administracion de Localidades");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Elija una Ubicacion:"));

        jCProvincia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCProvinciaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jCProvinciaMouseEntered(evt);
            }
        });
        jCProvincia.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCProvinciaItemStateChanged(evt);
            }
        });
        jCProvincia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCProvinciaActionPerformed(evt);
            }
        });
        jCProvincia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jCProvinciaKeyPressed(evt);
            }
        });

        jCDepartamento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jCDepartamentoMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jCDepartamentoMouseExited(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jCDepartamentoMouseReleased(evt);
            }
        });
        jCDepartamento.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCDepartamentoItemStateChanged(evt);
            }
        });
        jCDepartamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCDepartamentoActionPerformed(evt);
            }
        });
        jCDepartamento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jCDepartamentoKeyPressed(evt);
            }
        });

        jLabel1.setText("Provincia:");

        jLabel2.setText("Departamento:");

        jBAbreProvincias.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/agregar.gif"))); // NOI18N
        jBAbreProvincias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAbreProvinciasActionPerformed(evt);
            }
        });

        jBAbreDepartamentos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/agregar.gif"))); // NOI18N
        jBAbreDepartamentos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAbreDepartamentosActionPerformed(evt);
            }
        });

        jTNombreLocalidad1.setEnabled(false);
        jTNombreLocalidad1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTNombreLocalidad1KeyPressed(evt);
            }
        });

        jLabel4.setText("Nueva Localidad:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Localidades"
            }
        ));
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTNombreLocalidad1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .addComponent(jCDepartamento, 0, 233, Short.MAX_VALUE)
                    .addComponent(jCProvincia, 0, 233, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBAbreDepartamentos, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBAbreProvincias, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTNombreLocalidad1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBAbreProvincias, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBAbreDepartamentos, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(254, 254, 254))
        );

        jBGuardar.setText("Guardar");
        jBGuardar.setEnabled(false);
        jBGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBGuardarActionPerformed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jBEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/pllena.gif"))); // NOI18N
        jBEliminar.setToolTipText("eliminar un registro seleccionado del listado.");
        jBEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBEliminarMouseClicked(evt);
            }
        });
        jBEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarActionPerformed(evt);
            }
        });
        jBEliminar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBEliminarKeyPressed(evt);
            }
        });

        jBImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/printer.gif"))); // NOI18N
        jBImprimir.setToolTipText("imprimir un reporte del listado completo de productos.");
        jBImprimir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBImprimirMouseClicked(evt);
            }
        });
        jBImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBImprimirActionPerformed(evt);
            }
        });
        jBImprimir.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBImprimirKeyPressed(evt);
            }
        });

        jBModificar.setText("Modificar");
        jBModificar.setToolTipText("Permite guargar los cambios realizados al registro seleccionado.");
        jBModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBModificarActionPerformed(evt);
            }
        });
        jBModificar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBModificarKeyPressed(evt);
            }
        });

        jBNuevo.setText("Nuevo");
        jBNuevo.setToolTipText("Presione nuevo si desea agregar un nuevo registro.");
        jBNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNuevoActionPerformed(evt);
            }
        });
        jBNuevo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBNuevoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jBEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jBImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jBGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBGuardar, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jBEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jBModificar)
                    .addComponent(jBGuardar)
                    .addComponent(jBNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCProvinciaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCProvinciaItemStateChanged
        // TODO add your handling code here:
      
}//GEN-LAST:event_jCProvinciaItemStateChanged

    private void jCProvinciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCProvinciaActionPerformed
        this.llenarDepartamentos(this.jCProvincia.getSelectedItem().toString().trim());
}//GEN-LAST:event_jCProvinciaActionPerformed

    private void jCDepartamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCDepartamentoActionPerformed
        // TODO add your handling code here:    
//        System.out.println(this.jCDepartamento.getSelectedItem());
        if (this.jCDepartamento.getItemCount()>0){
            this.llenarLocalidades(this.jCDepartamento.getSelectedItem().toString().trim());
        }
    }//GEN-LAST:event_jCDepartamentoActionPerformed

    private void jCProvinciaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCProvinciaMouseEntered
        // TODO add your handling code here:
     
    }//GEN-LAST:event_jCProvinciaMouseEntered

    private void jCProvinciaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCProvinciaMouseClicked
        // TODO add your handling code here:
          
    }//GEN-LAST:event_jCProvinciaMouseClicked

    private void jCDepartamentoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCDepartamentoItemStateChanged
    }//GEN-LAST:event_jCDepartamentoItemStateChanged

    private void jCDepartamentoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCDepartamentoMouseReleased
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jCDepartamentoMouseReleased

    private void jCDepartamentoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCDepartamentoMouseEntered
        // TODO add your handling code here:
    
        
    }//GEN-LAST:event_jCDepartamentoMouseEntered

    private void jCDepartamentoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCDepartamentoMouseExited
        // TODO add your handling code here:

    }//GEN-LAST:event_jCDepartamentoMouseExited

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jBGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBGuardarActionPerformed
        // TODO add your handling code here:
            if (this.jTNombreLocalidad1.getText().equals("")){
                JOptionPane.showMessageDialog(null,"Para almacenar una nueva localidad debe escribir el nombre en Localidad ","Mensaje",JOptionPane.INFORMATION_MESSAGE);
            }else{
                this.guardar();
                this.jBNuevo.setEnabled(true);
                this.jBModificar.setEnabled(true);
             }
}//GEN-LAST:event_jBGuardarActionPerformed

    private void jBAbreProvinciasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAbreProvinciasActionPerformed
        // TODO add your handling code here:
        UINuevaProvincia ventProvincia=new UINuevaProvincia();
        ventProvincia.setVisible(true);
    }//GEN-LAST:event_jBAbreProvinciasActionPerformed

    private void jBAbreDepartamentosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAbreDepartamentosActionPerformed
        // TODO add your handling code here:
        UIDepartamento ventDepartamento=new UIDepartamento();
        ventDepartamento.setVisible(true);
    }//GEN-LAST:event_jBAbreDepartamentosActionPerformed

    private void jBEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBEliminarMouseClicked
        // TODO add your handling code here:
        if (jTable1.getSelectedRow()>=0){
            this.borrarRegistro(jTable1.getSelectedRow());
        }else{
            JOptionPane.showMessageDialog(null,"Debe seleccionar al menos un registro del listado","Mensaje",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jBEliminarMouseClicked

    private void jBEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jBEliminarActionPerformed

    private void jBEliminarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBEliminarKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
        this.borrarRegistro(jTable1.getSelectedRow());
    }//GEN-LAST:event_jBEliminarKeyPressed

    private void jBImprimirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBImprimirMouseClicked
        // TODO add your handling code here:
        cExport_thread report = new cExport_thread(3);
        report.start();
    }//GEN-LAST:event_jBImprimirMouseClicked

    private void jBImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBImprimirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jBImprimirActionPerformed

    private void jBImprimirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBImprimirKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
        if(evt.getKeyCode()==10){
            cExport_thread report = new cExport_thread(3);
            report.start();
        }
    }//GEN-LAST:event_jBImprimirKeyPressed

    private void jBModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBModificarActionPerformed
        // TODO add your handling code here:
        if (this.jTable1.getSelectedRow()>=0){
            this.modificar();
        }else{
            JOptionPane.showMessageDialog(null,"Debe seleccionar al menos un registro del listado","Mensaje",JOptionPane.WARNING_MESSAGE);
        }
            
    }//GEN-LAST:event_jBModificarActionPerformed

    private void jBModificarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBModificarKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jBModificarKeyPressed

    private void jBNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNuevoActionPerformed
        // TODO add your handling code here:
        this.guardar = true;
        this.jBGuardar.setEnabled(true);        
        this.jBNuevo.setEnabled(false);
        this.jBModificar.setEnabled(false);
        this.jTNombreLocalidad1.setText("");
        this.jTNombreLocalidad1.requestFocus();
    }//GEN-LAST:event_jBNuevoActionPerformed

    private void jBNuevoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBNuevoKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
        if(evt.getKeyCode()==10) {
            this.guardar = true;
            this.jBGuardar.setEnabled(true);
            this.jTNombreLocalidad1.setText("");
        }
    }//GEN-LAST:event_jBNuevoKeyPressed

    private void jTNombreLocalidad1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTNombreLocalidad1KeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
    }//GEN-LAST:event_jTNombreLocalidad1KeyPressed

    private void jCProvinciaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCProvinciaKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
    }//GEN-LAST:event_jCProvinciaKeyPressed

    private void jCDepartamentoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCDepartamentoKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
    }//GEN-LAST:event_jCDepartamentoKeyPressed

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
    }//GEN-LAST:event_jTable1KeyPressed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UIEligeDireccion().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAbreDepartamentos;
    private javax.swing.JButton jBAbreProvincias;
    private javax.swing.JButton jBEliminar;
    private javax.swing.JButton jBGuardar;
    private javax.swing.JButton jBImprimir;
    private javax.swing.JButton jBModificar;
    private javax.swing.JButton jBNuevo;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jCDepartamento;
    private javax.swing.JComboBox jCProvincia;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTNombreLocalidad1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    
    private void llenarProvincias(){
        
        CListar listar = new CListar();
        provincias = listar.hacerListado(new Provincia());
        listar = null;
        Iterator it = provincias.iterator();
        Provincia p = new Provincia();
        while(it.hasNext())
        {
            p = (Provincia)it.next();
            this.jCProvincia.addItem(p.getNombre().trim());
        }
        p = null;
        it = null;
        
        this.llenarDepartamentos(this.jCProvincia.getSelectedItem().toString().trim());        
    }
    private void llenarDepartamentos(String nombreProvincia){
        //Obtengo el id de provincia en elobjeto completo
        this.vaciarDepartamentos();
//        this.vaciarLocalidades();
        this.limpiar();
        
        Consulta con=new Consulta();
        Provincia prov=con.getIdProvincia(nombreProvincia);
        
        //Hago ellistado de departamentos por provincia
        Consulta con2=new Consulta();
        departamentos = con2.getDepartamentoxProv(prov.getIdprovincia());
        
//        System.out.println("En DEPTO hay cargados:"+this.jCDepartamento.getItemCount());
        
        if (!departamentos.isEmpty()){
            Iterator it = departamentos.iterator();
            Departamento d = new Departamento();
            while(it.hasNext())
            {
                d = (Departamento)it.next();
                this.jCDepartamento.addItem(d.getNombre().trim());
            }
            d = null;
            it = null;
            this.jTNombreLocalidad1.setEnabled(true);
            this.jBNuevo.setEnabled(true);
             this.jBModificar.setEnabled(true);
            this.llenarLocalidades(this.jCDepartamento.getSelectedItem().toString().trim());
        }else{
             JOptionPane.showMessageDialog(null,"No existen departamentos cargados en la provincia seleccionada,cargue una para ingresar una localidad","Faltan Datos",JOptionPane.INFORMATION_MESSAGE);
             this.jTNombreLocalidad1.setEnabled(false);
             this.jBNuevo.setEnabled(false);
             this.jBModificar.setEnabled(false);
        }
        
    }
    private void llenarLocalidades(String nombreDepartamento){
        //Obtengo el id de provincia en elobjeto completo
        this.limpiar();
        Consulta con=new Consulta();
        Departamento dep=con.getIdDepartamento(nombreDepartamento);
        
        
//      Hago ellistado de departamentos por provincia
        Consulta con2=new Consulta();
        localidades = con2.getLocalidadxDep(dep.getIddepartamento());
        
//        if  (localidades.isEmpty()){
//            System.out.println("esnulo las localidades collection");
//        } else {
//            System.out.println("localidades tiene algo");
//        }
        Iterator it = localidades.iterator();
        Localidad l = new Localidad();
        
        DefaultTableModel modelo = (DefaultTableModel)this.jTable1.getModel();
        String datos[] = new String[1];
        
        
        while(it.hasNext())
        {
            l = (Localidad)it.next();
            datos[0] = l.getNombre();
            modelo.addRow(datos);
//            this.jCLocalidad.addItem(l.getNombre().trim());
        }
        l = null;
        it = null;
    }
    private void vaciarDepartamentos(){
        this.jCDepartamento.removeAllItems();
    }
//    private void vaciarLocalidades(){
//        this.jCLocalidad.removeAllItems();
//    }
    private void limpiar()
    {
        DefaultTableModel modelo = (DefaultTableModel)this.jTable1.getModel();
        while(this.jTable1.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
    }
    private void guardar()
    {
        int bandera = 0; // esta bandera es para saber si es que selecciono alguna localidad
//        String nombreDepartamento = (this..getModel().getColumnName(0).trim());
        String nombreDepartamento = this.jCDepartamento.getSelectedItem().toString().trim();        
        
        Iterator it = departamentos.iterator();
        Localidad localidad = new Localidad();
        Departamento departamento = new Departamento();
        Departamento d = new Departamento();
        while(it.hasNext())
        {
            d = (Departamento)it.next();
            if(nombreDepartamento.equals(d.getNombre().trim()))
            {
                departamento = d;
                bandera++;
            }
        }
        localidad.setExiste("S");
        localidad.setNombre(this.jTNombreLocalidad1.getText().toUpperCase().trim());
        localidad.setDepartamento(departamento);
        
//        System.out.println("nombreLocalidad:"+localidad.getNombre());
//        System.out.println("idDeptode esa Localidad:"+localidad.getDepartamento().getIddepartamento());
        
        this.guardar = false;
        this.jBGuardar.setEnabled(false);
        // aqui va el codigo que guarda en la base de datos
        CAlta alta = new CAlta();
        int respuesta = alta.hacerAlta(localidad);
        if(respuesta != 0)
                JOptionPane.showMessageDialog(null,"Error en la carga de datos","Error",JOptionPane.ERROR_MESSAGE);
        else
                JOptionPane.showMessageDialog(null,"Los datos se cargaron correctamente","Carga Finalizada",JOptionPane.INFORMATION_MESSAGE);
//        this.vaciarLocalidades();
        this.limpiar();
        this.llenarLocalidades(localidad.getDepartamento().getNombre().trim());
        this.jTNombreLocalidad1.setText("");
    }
    private void borrarRegistro(int fila)
    {
        Iterator it = localidades.iterator();
        Departamento dep=new Departamento();
        
        while(it.hasNext())
        {
            Localidad l = (Localidad)it.next();
            if(l.getNombre().equals(String.valueOf(this.jTable1.getValueAt(fila,0))))
            {
                Baja baja = new Baja();
                dep=l.getDepartamento();
                int respuesta = baja.eliminar(l);
                baja = null;
                if(respuesta == 0)
                    JOptionPane.showMessageDialog(null,"Error al dar de baja la localidad","Error",JOptionPane.ERROR_MESSAGE);
                else
                    JOptionPane.showMessageDialog(null,"La localidad se elimino correctamente","Baja Completada",JOptionPane.INFORMATION_MESSAGE);
                    
            }
            l = null;
        }
        it = null;
        this.limpiar();
        this.llenarLocalidades(dep.getNombre());
        
    }
    private void modificar()
    {
        int bandera = 0; // esta bandera es para saber si es que selecciono alguna localidad
        String nombreDepartamento = this.jCDepartamento.getSelectedItem().toString().trim();
                
        Iterator it = departamentos.iterator();        
        Localidad localidad = new Localidad();
        Departamento departamento = new Departamento();
        Departamento d = new Departamento();
        while(it.hasNext())
        {
            d = (Departamento)it.next();
            if(nombreDepartamento.equals(d.getNombre().trim()))
            {
                departamento = d;
                bandera++;
            }
        }
        
        Localidad loc=new Localidad();
        Iterator it2 = localidades.iterator();        
        while(it2.hasNext())
        {
            Localidad l = (Localidad)it2.next();
            if(l.getNombre().trim().equals(String.valueOf(this.jTable1.getValueAt(this.jTable1.getSelectedRow(),0)).trim()))
            {             
                    loc=l;                     
//                    System.out.println("iteratorGG"+l.getIdlocalidad());
            }            
            //l = null;
        }
        it2 = null;
        localidad.setExiste("S");
        localidad.setNombre(this.jTNombreLocalidad1.getText().toUpperCase().trim());        
        localidad.setDepartamento(departamento);        
        localidad.setIdlocalidad(loc.getIdlocalidad());        
        this.guardar = false;
//        this.jBGuardar.setEnabled(false);
        // aqui va el codigo que guarda en la base de datos
        Modificacion mod = new Modificacion();
        int respuesta = mod.modificar(localidad);
        if(respuesta == 0)
                JOptionPane.showMessageDialog(null,"Error en la carga de datos","Error",JOptionPane.ERROR_MESSAGE);
        else
                JOptionPane.showMessageDialog(null,"Los datos se modificaron correctamente","Modificacion Finalizada",JOptionPane.INFORMATION_MESSAGE);
        this.limpiar();
        this.llenarLocalidades(localidad.getDepartamento().getNombre().trim());
        this.jTNombreLocalidad1.setText("");
    }
}

